﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 日志服务
    /// </summary>
    public class LogService : ILogService
    {
        private readonly DbContext dal = new DbContext();

        public bool AddBusinessLog(T_BusinessExceptionLog model)
        {
            return dal.Insert(model);
        }

        public bool AddErrorLog(T_ErrorLog model)
        {
            return dal.Insert(model);
        }

        public T_BusinessExceptionLog GetBusinessLog(string id)
        {
            return dal.Get<T_BusinessExceptionLog>(id);
        }

        public T_ErrorLog GetErrorLog(string id)
        {
            return dal.Get<T_ErrorLog>(id);
        }

        public PageData<T_BusinessExceptionLog> Query(string ip, string userName, string content, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_BusinessExceptionLog, bool>> whereExp = (a) => true;
            if (!string.IsNullOrWhiteSpace(ip))
            {
                whereExp = whereExp.Compose((a) => a.ClientIP.StartsWith(ip), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(userName))
            {
                whereExp = whereExp.Compose((a) => a.UserName.StartsWith(userName), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(content))
            {
                whereExp = whereExp.Compose((a) => a.Content.StartsWith(content), Expression.AndAlso);
            }

            List<T_BusinessExceptionLog> list = dal.Queryable<T_BusinessExceptionLog>().Where(whereExp).OrderBy(a => a.CreateTime, SqlSugar.OrderByType.Desc).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_BusinessExceptionLog> { Data = list, Page = page };
        }

        public PageData<T_ErrorLog> Query(string ip, string userName, string msg, string source, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_ErrorLog, bool>> whereExp = (a) => true;
            if (!string.IsNullOrWhiteSpace(ip))
            {
                whereExp = whereExp.Compose((a) => a.ClientIP.StartsWith(ip), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(userName))
            {
                whereExp = whereExp.Compose((a) => a.UserName.StartsWith(userName), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(msg))
            {
                whereExp = whereExp.Compose((a) => a.Message.StartsWith(msg), Expression.AndAlso);
            }

            List<T_ErrorLog> list = dal.Queryable<T_ErrorLog>().Where(whereExp).OrderBy(a => a.CreateTime, SqlSugar.OrderByType.Desc).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_ErrorLog> { Data = list, Page = page };
        }
    }
}
